﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data

Namespace Arwani
    Partial Class Backend_ManageProducts
        Inherits System.Web.UI.Page
        Public Function getImage(ByVal imgSrc As String) As String
            Dim strReturn As String = "../images/main_banner.png"
            If File.Exists(Server.MapPath("../Products/" & imgSrc)) Then
                strReturn = "../Products/" & imgSrc
            End If
            Return strReturn
        End Function

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Session("ADMIN_USER") IsNot Nothing) Then
                If Page.IsPostBack Then Exit Sub
                Call loadData("")
                Call loadBrands()

            Else
                Response.Redirect("Default.aspx")
            End If

        End Sub
        Public Sub loadBrands()
            Dim arwCon As New ArwaniController
            Dim strQ As String = ""
            strQ = "select * from brand order by brandname"
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.cboBrands.DataSource = dtTable
            Me.cboBrands.DataTextField = "brandname"
            Me.cboBrands.DataValueField = "brandid"
            Me.cboBrands.DataBind()
            Me.cboBrands.Items.Insert(0, New ListItem("Any Brand", 0))
        End Sub
        Public Sub loadData(ByVal strFilter As String)
            Dim arwCon As New ArwaniController
            Dim strQ As String = ""
            If strFilter <> "" Then
                If Me.cboBrands.SelectedIndex > 0 Then
                    strQ = "select * from product where prodname like '%" & strFilter & "%' and brandid=" & Me.cboBrands.SelectedItem.Value & " order by prodname"
                Else
                    strQ = "select * from product where prodname like '%" & strFilter & "%' order by prodname"
                End If

            Else
                If Me.cboBrands.SelectedIndex > 0 Then
                    strQ = "select * from product where   brandid=" & Me.cboBrands.SelectedItem.Value & " order by prodname"
                Else
                    strQ = "select * from product order by prodname"
                End If

            End If


            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.dgProducts.DataSource = dtTable
            Me.dgProducts.DataBind()

        End Sub

        Public Function getDesc(ByVal desc As String) As String
            Return desc.Replace("_x000d_", "")
        End Function

        Protected Sub dgProducts_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgProducts.ItemCommand
            If e.CommandName = "Images" Then
                Response.Redirect("ManageGallery.aspx?pid=" & CInt(e.CommandArgument))
            ElseIf e.CommandName = "Edit" Then
                Response.Redirect("EditProduct.aspx?pid=" & CInt(e.CommandArgument))
            ElseIf e.CommandName = "Featured" Then
                FeatureSetting(CInt(e.CommandArgument.ToString()), getFeatured(CInt(e.CommandArgument.ToString())))
            ElseIf e.CommandName = "Delete" Then
                Dim arwCon As New ArwaniController
                Dim strQ As String = ""
                strQ = "delete from product where productid=" & CInt(e.CommandArgument)
                arwCon.ExecuteNonQuery(strQ)
                Call loadData(Me.txtSearch.Text)
            End If
        End Sub

        Public Function getFeatured(ByVal pid As Integer) As Integer
            Dim strQ As String = "select * from product where productid=" & CInt(pid)
            Dim arwCon As New ArwaniController
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                Return dtTable.Rows(0)("isfeatured")
            End If
            Return 0
        End Function

        Public Sub FeatureSetting(ByVal pid As Integer, ByVal isFeatured As Integer)
            Dim strQ As String = ""
            If isFeatured = 1 Then
                strQ = "update product set isfeatured =0 where productid=" & pid
            Else
                strQ = "update product set isfeatured =1 where productid=" & pid
            End If
            Dim arwCon As New ArwaniController
            arwCon.ExecuteNonQuery(strQ)
            Response.Redirect("ManageProducts.aspx")
        End Sub

        Protected Sub dgProducts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgProducts.ItemDataBound
            Try
                Dim lnkDelete As LinkButton = CType(e.Item.FindControl("lnkDelete"), LinkButton)
                lnkDelete.Attributes.Add("onclick", "return confirm('Confirm delete this product?')")

                Dim lnkFeatured As LinkButton = CType(e.Item.FindControl("lnkFeatured"), LinkButton)
                lnkFeatured.Text = getIsFeatured(lnkFeatured.CommandArgument)

                Dim productID As Integer = dgProducts.DataKeys(e.Item.ItemIndex)
                Dim txtSortOrder As TextBox = CType(e.Item.FindControl("txtSortOrder"), TextBox)
                txtSortOrder.Attributes.Add("onblur", "postSortOrder(this.value,'product', 'productID','" & productID & "')")
            Catch ex As Exception

            End Try
        End Sub
        Public Function getBrandName(ByVal BrandID As Integer) As String
            Dim arwCon As New ArwaniController
            Dim strQ As String = ""
            strQ = "select * from brand where brandid=" & BrandID
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                If Not IsDBNull(dtTable.Rows(0)("brandname")) Then Return dtTable.Rows(0)("brandname")
            End If
            Return "N/A"
        End Function
        Public Function getIsFeatured(ByVal pid As Integer) As String
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from product where productid=" & CInt(pid)
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            If dtTable.Rows.Count > 0 Then
                If CBool(dtTable.Rows(0)("isfeatured")) Then
                    Return "Mark Un-Featured"
                End If
                Return "Mark Featured"
            End If
            Return "Mark Featured"
        End Function
        'Me.dgProducts.CurrentPageIndex = e.NewPageIndex
        'Call loadData()
        '
         
        Protected Sub lnkAddNewProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAddNewProduct.Click
            Response.Redirect("EditProduct.aspx?pid=0")
        End Sub
         
        Protected Sub dgProducts_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgProducts.PageIndexChanged
            Me.dgProducts.CurrentPageIndex = e.NewPageIndex
            Call loadData(Me.txtSearch.Text)
        End Sub

        Protected Sub lnkSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSearch.Click

            Call loadData(Me.txtSearch.Text)

        End Sub
    End Class
End Namespace